package util

import (
	"context"

	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"
)

func CheckToken(ctx context.Context) error {
	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		return grpc.Errorf(codes.Unauthenticated, "无Token认证信息")
	}

	var (
		appId  string
		appKey string
	)
	if val, ok := md["appId"]; ok {
		appId = val[0]
	}
	if val, ok := md["appKey"]; ok {
		appKey = val[0]
	}
	if appId != "101010" || appKey != "i am key" {
		return grpc.Errorf(codes.Unauthenticated, "Token认证信息无效: appId=%s, appKey=%s", appId, appKey)
	}
	return nil
}
